# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
#
# Testing
# RoleDef/Role and Dependencies
#              Proxy
#              /  |
#           Head  |            
#             \   |
#            Workers (2 of them)
#
# VMs
# VM0: Proxy+Head
# VM1: Worker1
# VM2: Worker2
#
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #

from crv.model import *
from crv.tools.crvtools import buildRoleDep

def createSampleData(metadata,session):
    (rp,rpprivate, rw1, rw2)=createSampleResourcePoolsMagellan(metadata,session)
#    (rp,rpprivate)=createSampleResourcePoolsAcs(metadata,session)    
    createSampleCluster(metadata,session,rp,rpprivate, rw1, rw2)
    
def createSampleResourcePoolsAcs(metadata,session):
    
    #Resource Pool

    acs_euca=EucaVMMaker("acs_euca_public")
    acs_euca.setAttr('doc',"VMs in ACS Eucalyptus Cluster with Public IP")
    
    acs_euca.setAttr('EC2_URL', "http://131.243.2.81:8773/services/Eucalyptus")
#yyao
#    acs_euca.setAttr('EC2_ACCESS_KEY', 'OkWHUDFUw6WvvdeVF8OuUbXK8J3dENa6BDV8Q')
#    acs_euca.setAttr('EC2_SECRET_KEY','Wc1qcKlYPfDxFp2cC1uCuI3qClWNIAWQFKzsEw')
#    acs_euca.setAttr('keyname', 'yushu_key')
#    acs_euca.setAttr('group_names', 'yushu_at3' )
#yyao-secure
    acs_euca.setAttr('EC2_ACCESS_KEY', 'e8d5EXIyHFbCwhVzgC6aXRewwioEbN1qqJEXGg')
    acs_euca.setAttr('EC2_SECRET_KEY','qXiuO550FvWiiMc5SxZyp5u2rPOjpMl0QVsA')
    acs_euca.setAttr('keyname', 'yushu_secure_key')
    acs_euca.setAttr('group_names', 'yushu_secure_at3' )
    
    acs_euca.setAttr('image_id', 'emi-FDCC153E')    
    acs_euca.setAttr('kernel_id', 'eki-AE5117CB')
    acs_euca.setAttr('ramdisk_id', 'eri-16D3191E')
    acs_euca.setAttr('instance_type', 'm1.small' )
    acs_euca.setAttr('addressing_type', "public")
    
    cpc=CentOS5_Puppet_ClientMaker(name="RHEL5_Puppet",doc="CRVClient for RHEL5 Clients using Puppet",templatedir="/opt/cloudcrv/cloudcrv/crvclient")
    ppm=Puppet_ProfileMaker(name="Puppet",doc="Profile Maker using Puppet")

    rp=RP("publicIP","Resource Pool with Publicly Addressed VMs, Puppet Profiles and RHEL5 Clients",profilemaker=ppm,vmmaker=acs_euca,clientmaker=cpc)
    
    session.add(rp)

    acs_euca=EucaVMMaker("acs_euca_private")
    acs_euca.setAttr('doc',"VMs in ACS Eucalyptus Cluster with only Private IP")
    
    acs_euca.setAttr('EC2_URL', "http://131.243.2.81:8773/services/Eucalyptus")
#yyao
#    acs_euca.setAttr('EC2_ACCESS_KEY', 'OkWHUDFUw6WvvdeVF8OuUbXK8J3dENa6BDV8Q')
#    acs_euca.setAttr('EC2_SECRET_KEY','Wc1qcKlYPfDxFp2cC1uCuI3qClWNIAWQFKzsEw')
#    acs_euca.setAttr('keyname', 'yushu_key')
#    acs_euca.setAttr('group_names', 'yushu_at3' )
#yyao-secure
    acs_euca.setAttr('EC2_ACCESS_KEY', 'e8d5EXIyHFbCwhVzgC6aXRewwioEbN1qqJEXGg')
    acs_euca.setAttr('EC2_SECRET_KEY','qXiuO550FvWiiMc5SxZyp5u2rPOjpMl0QVsA')
    acs_euca.setAttr('keyname', 'yushu_secure_key')
    acs_euca.setAttr('group_names', 'yushu_secure_at3' )
    
    acs_euca.setAttr('image_id', 'emi-FDCC153E')
    acs_euca.setAttr('kernel_id', 'eki-AE5117CB')
    acs_euca.setAttr('ramdisk_id', 'eri-16D3191E')
    acs_euca.setAttr('instance_type', 'm1.small' )
    acs_euca.setAttr('addressing_type', "private")

    rpprivate=RP("privateIP","Resource Pool with Privatly Addressed VMs, Puppet Profiles and RHEL5 Clients",profilemaker=ppm,vmmaker=acs_euca,clientmaker=cpc)

    session.add(rpprivate)

    return rp, rpprivate

def createSampleResourcePoolsMagellan(metadata,session):
    
    #Resource Pool

    magellan_euca=EucaVMMaker("magellan_euca")
    
    magellan_euca.setAttr('EC2_URL', "http://mageuca.nersc.gov:8773/services/Eucalyptus")
    magellan_euca.setAttr('EC2_ACCESS_KEY', 'OkWHUDFUw6WvvdeVF8OuUbXK8J3dENa6BDV8Q')
    magellan_euca.setAttr('EC2_SECRET_KEY','KEpZfoxFhpzeObx7ZyjX47RdUDx4MCB6cNRCuQ')
    
    magellan_euca.setAttr('image_id', 'emi-A8301419')    
    magellan_euca.setAttr('keyname', 'yushu_newkey')
    magellan_euca.setAttr('kernel_id', 'eki-A86F17CD')
    magellan_euca.setAttr('ramdisk_id', 'eri-1062190B')
    magellan_euca.setAttr('instance_type', 'm1.large' )
    magellan_euca.setAttr('group_names', 'yushu_group' )
    magellan_euca.setAttr('addressing_type', "public")

    cpc=CentOS5_Puppet_ClientMaker(name="RHEL5_Puppet",doc="CRVClient for RHEL5 Clients using Puppet",templatedir="/opt/cloudcrv/cloudcrv/crvclient")
    ppm=Puppet_ProfileMaker(name="Puppet",doc="Profile Maker using Puppet")

    rp=RP("publicIP","Resource Pool with Publicly Addressed VMs, Puppet Profiles and RHEL5 Clients",profilemaker=ppm,vmmaker=magellan_euca,clientmaker=cpc)

    session.add(rp)

    magellan_euca=EucaVMMaker("magellan_euca")
    
    magellan_euca.setAttr('EC2_URL', "http://mageuca.nersc.gov:8773/services/Eucalyptus")
    magellan_euca.setAttr('EC2_ACCESS_KEY', 'OkWHUDFUw6WvvdeVF8OuUbXK8J3dENa6BDV8Q')
    magellan_euca.setAttr('EC2_SECRET_KEY','KEpZfoxFhpzeObx7ZyjX47RdUDx4MCB6cNRCuQ')
    
    magellan_euca.setAttr('image_id', 'emi-A8301419')    
    magellan_euca.setAttr('keyname', 'yushu_newkey')
    magellan_euca.setAttr('kernel_id', 'eki-A86F17CD')
    magellan_euca.setAttr('ramdisk_id', 'eri-1062190B')
    magellan_euca.setAttr('instance_type', 'm1.large' )
    magellan_euca.setAttr('group_names', 'yushu_group' )
    magellan_euca.setAttr('addressing_type', "private")

    cpc=CentOS5_Puppet_ClientMaker(name="RHEL5_Puppet",doc="CRVClient for RHEL5 Clients using Puppet",templatedir="/opt/cloudcrv/cloudcrv/crvclient")
    ppm=Puppet_ProfileMaker(name="Puppet",doc="Profile Maker using Puppet")

    rpprivate=RP("privateIP","Resource Pool with Private Address VMs, Puppet Profiles and RHEL5 Clients",profilemaker=ppm,vmmaker=magellan_euca,clientmaker=cpc)

    session.add(rpprivate)

    magellan_euca=EucaVMMaker("magellan_euca")
    
    magellan_euca.setAttr('EC2_URL', "http://mageuca.nersc.gov:8773/services/Eucalyptus")
    magellan_euca.setAttr('EC2_ACCESS_KEY', 'OkWHUDFUw6WvvdeVF8OuUbXK8J3dENa6BDV8Q')
    magellan_euca.setAttr('EC2_SECRET_KEY','KEpZfoxFhpzeObx7ZyjX47RdUDx4MCB6cNRCuQ')
    
    magellan_euca.setAttr('image_id', 'emi-A8301419')    
    magellan_euca.setAttr('keyname', 'yushu_newkey')
    magellan_euca.setAttr('kernel_id', 'eki-A86F17CD')
    magellan_euca.setAttr('ramdisk_id', 'eri-1062190B')
    magellan_euca.setAttr('instance_type', 'm1.small' )
    magellan_euca.setAttr('group_names', 'yushu_worker1_group' )
    magellan_euca.setAttr('addressing_type', "private")

    cpc=CentOS5_Puppet_ClientMaker(name="RHEL5_Puppet",doc="CRVClient for RHEL5 Clients using Puppet",templatedir="/opt/cloudcrv/cloudcrv/crvclient")
    ppm=Puppet_ProfileMaker(name="Puppet",doc="Profile Maker using Puppet")

    rpprivate_worker1=RP("privateIP_worker1","Resource Pool with Private Address VMs, Puppet Profiles and RHEL5 Clients",profilemaker=ppm,vmmaker=magellan_euca,clientmaker=cpc)

    session.add(rpprivate_worker1)

    magellan_euca=EucaVMMaker("magellan_euca")
    
    magellan_euca.setAttr('EC2_URL', "http://mageuca.nersc.gov:8773/services/Eucalyptus")
    magellan_euca.setAttr('EC2_ACCESS_KEY', 'OkWHUDFUw6WvvdeVF8OuUbXK8J3dENa6BDV8Q')
    magellan_euca.setAttr('EC2_SECRET_KEY','KEpZfoxFhpzeObx7ZyjX47RdUDx4MCB6cNRCuQ')
    
    magellan_euca.setAttr('image_id', 'emi-A8301419')    
    magellan_euca.setAttr('keyname', 'yushu_newkey')
    magellan_euca.setAttr('kernel_id', 'eki-A86F17CD')
    magellan_euca.setAttr('ramdisk_id', 'eri-1062190B')
    magellan_euca.setAttr('instance_type', 'm1.small' )
    magellan_euca.setAttr('group_names', 'yushu_worker2_group' )
    magellan_euca.setAttr('addressing_type', "private")

    cpc=CentOS5_Puppet_ClientMaker(name="RHEL5_Puppet",doc="CRVClient for RHEL5 Clients using Puppet",templatedir="/opt/cloudcrv/cloudcrv/crvclient")
    ppm=Puppet_ProfileMaker(name="Puppet",doc="Profile Maker using Puppet")

    rpprivate_worker2=RP("privateIP_worker2","Resource Pool with Private Address VMs, Puppet Profiles and RHEL5 Clients",profilemaker=ppm,vmmaker=magellan_euca,clientmaker=cpc)

    session.add(rpprivate_worker2)


    return rp, rpprivate, rpprivate_worker1, rpprivate_worker2

def createSampleCluster(metadata,session,rp,rpprivate, rpprivate_worker1,rpprivate_worker2):

    #Role Definitions
    rdGangliaSrv=RoleDef('GangliaSrv','Ganglia Server (MetaDaemon and WebFront)','at3_ganglia_srv',"puppet")

    rdGangliaClient=RoleDef('GangliaClient','Ganglia Client (Monitoring Daemon)','at3_ganglia_daemon',"puppet")
    rdGangliaClient.addDependOn(rdGangliaSrv)

#    rdNFSSrv=RoleDef('NFSSrv',"NFS Server","at3_nfssrv","puppet")
#    rdNFSSrv.addDependOn(rdGangliaClient,local=True)
    
    rdLDAPSrv=RoleDef('LDAPSrv',"LDAP Server","at3_ldapserver","puppet")
#    rdLDAPSrv.addDependOn(rdNFSClient,local=True)
    rdLDAPSrv.addDependOn(rdGangliaClient,local=True)
    
    rdLDAPClient=RoleDef('LDAPClient',"LDAP Linux Auth Client","at3_ldapclient","puppet")
    rdLDAPClient.addDependOn(rdLDAPSrv)
    
#    rdNFSClient=RoleDef('NFSClient',"NFS Client","at3_nfsclient","puppet")
#    rdNFSClient.addDependOn(rdNFSSrv)

    rdProxy=RoleDef('Proxy',"Proxy Server","at3_proxy","puppet")
#    rdProxy.addDependOn(rdNFSClient,local=True)
    rdProxy.addDependOn(rdGangliaClient,local=True)
    
    rdCVMFSClient=RoleDef('CVMFSClient',"CVMFS Client","at3_cvmfs","puppet")
    rdCVMFSClient.addDependOn(rdProxy)

    rdHead=RoleDef('Head',"Condor Head","at3_head","puppet")
    rdHead.addDependOn(rdLDAPClient,local=True)
#    rdHead.addDependOn(rdNFSClient,local=True)
    rdHead.addDependOn(rdGangliaClient,local=True)
    rdHead.addDependOn(rdProxy)

    rdInteractive=RoleDef('Interactive',"Condor Interactive","at3_interactive","puppet")
    rdInteractive.addDependOn(rdLDAPClient,local=True)    
#    rdInteractive.addDependOn(rdNFSClient,local=True)
    rdInteractive.addDependOn(rdCVMFSClient,local=True)
    rdInteractive.addDependOn(rdGangliaClient,local=True)
    rdInteractive.addDependOn(rdHead)

    rdWorker=RoleDef('Worker',"Condor Worker","at3_worker","puppet")
    rdWorker.addDependOn(rdLDAPClient,local=True)    
#    rdWorker.addDependOn(rdNFSClient,local=True)    
    rdWorker.addDependOn(rdCVMFSClient,local=True)
    rdWorker.addDependOn(rdGangliaClient,local=True)
    rdWorker.addDependOn(rdHead)
    
    cl=Cluster("tier3")
    cl.setAttr("confdir","/opt/cloudcrv/puppet")

    rGangliaSrv=cl.addRole(name="rGangliaSrv", roledef=rdGangliaSrv, vm=rp.newVM("vmGangliaSrv"), enabled=True)

#    rNFSSrv=cl.addRole(name="rNFSSrv", roledef=rdNFSSrv, vm=rpprivate.newVM("vmNFSSrv"), enabled=True)
#    rNFSSrv.setAttr("NFSServerSharePath","/export/share")
#    rNFSSrv.setAttr("NFSServerClientMountPath","/export/share")

    rLDAPSrv=cl.addRole(name="rLDAPSrv", roledef=rdLDAPSrv, vm=rpprivate.newVM("vmLDAPSrv"),enabled=True)
    rLDAPSrv.setAttr("rootpw","{SSHA}Rq03UQ2HtDnqozwigoXEESAaqMaXq5cr")
    rLDAPSrv.setAttr("suffix","dc=mytier3,dc=com")
    
    rProxy=cl.addRole(name="rProxy", roledef=rdProxy, vm=rpprivate.newVM("vmProxy"), enabled=True)

    rHead=cl.addRole(name="rHead",roledef=rdHead,vm=rpprivate.newVM("vmHead"),enabled=True)
    rHead.setAttr("FILESYSTEM_DOMAIN","atlastier3")
    rHead.setAttr("CondorPassword","abcdefg")
    
    for i in range(0):
        rr=cl.addRole(name="rInteractive%02d"%i,roledef=rdInteractive,vm=rp.newVM("vmInteractive%02d"%i),enabled=True)
        rr.setAttr("CondorPassword","abcdefg")

    nWorkers=2

    for i in range(nWorkers):
        if i%2 == 0:
            rr=cl.addRole(name="rWorker%03d"%i, roledef=rdWorker, vm=rpprivate_worker1.newVM("vmWorker%03d"%i), enabled=True)
        else:
            rr=cl.addRole(name="rWorker%03d"%i, roledef=rdWorker, vm=rpprivate_worker2.newVM("vmWorker%03d"%i), enabled=True)

        rr.setAttr("CondorPassword","abcdefg")
    cl.setAttr("numWorkers", nWorkers);
    
    buildRoleDep(cl.role)

    session.add(cl)

    
